import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/utils/event.dart';
import 'package:star_author/utils/rouces_util.dart';
import 'package:star_author/widgets/constraints_widget.dart';
import 'package:star_author/widgets/qr_code_widget.dart';

class SearchInputWidget extends StatefulWidget {
  static const String refresh_search_input_widget_nodes =
      "refresh_search_input_widget_nodes";
  late String? initVal;
  late dynamic width;
  late String placeholder;
  late bool enabled;
  late bool autoFocus;
  late Function()? onTap;
  late Function(String)? onConfirm;
  late Function(String)? onChanged;
  late Color color;
  late bool isSearchPage;
  late bool isScan;
  final String entrance;
  final TextAlign? textAlign;

  SearchInputWidget({
    Key? key,
    this.initVal,
    this.width = 100.0,
    this.placeholder = '',
    this.enabled = true,
    this.autoFocus = false,
    this.onTap,
    this.onConfirm,
    this.onChanged,
    this.color = const Color(0xFFf6f6f6),
    this.isSearchPage = false,
    this.isScan = false,
    this.entrance = QRCodeDialog.bidderDetail,
    this.textAlign = TextAlign.left,
  }) : super(key: key);

  _SearchInputWidgetState createState() => _SearchInputWidgetState();
}

class _SearchInputWidgetState extends State<SearchInputWidget> {
  double height = 30.0;
  late TextEditingController controller =
      TextEditingController(text: widget.initVal);
  late FocusNode focusNode = FocusNode();
  late BoxConstraints constraints = BoxConstraints(
    minWidth: 30,
    minHeight: height.w,
  );

  @override
  void initState() {
    super.initState();
    Event.on(SearchInputWidget.refresh_search_input_widget_nodes, (arg) {
      if (arg != null) {
        if (arg['autoFocus'] ?? false) {
          if (widget.autoFocus) {
            focusNode.requestFocus();
            controller.clear();
          }
        } else {
          focusNode.unfocus();
        }
      }
    });
    if (widget.autoFocus) {
      focusNode.requestFocus();
    }
  }

  @override
  void dispose() {
    controller.dispose();
    focusNode.dispose();
    super.dispose();
  }

  Widget? buildSuffixIcon() {
    if (controller.text.isNotEmpty) {
      return GestureDetector(
        child: Icon(
          Icons.cancel,
          color: ColorConf.lightGray,
          size: 20.w,
        ),
        onTap: () {
          if (!widget.isSearchPage) {
            FocusScope.of(context).unfocus();
          }
          controller.clear();
          setState(() {});
          widget.onConfirm?.call('');
        },
      );
    } else if (widget.isScan) {
      return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          QRCodeUtil.show(context, entrance: widget.entrance);
        },
        child: Padding(
          padding:
              EdgeInsets.only(right: 8.w, top: 5.w, bottom: 5.w, left: 20.w),
          child: RoucesUtil.getAssetsIcon('ic_scan_lt', 16.w),
        ),
      );
    }
    return Icon(
      Icons.cancel,
      color: ColorConf.transparent,
      size: 20.w,
    );
  }

  @override
  Widget build(BuildContext context) {
    return ConstraintsWidget(
      width: (widget.width as double).w,
      child: Container(
        height: height.w,
        alignment: Alignment.centerLeft,
        decoration: BoxDecoration(
          color: widget.color,
          borderRadius: BorderRadius.all(Radius.circular(50)),
        ),
        child: TextFormField(
          focusNode: focusNode,
          onTap: widget.onTap,
          onFieldSubmitted: widget.onConfirm,
          onChanged: widget.onChanged,
          controller: controller,
          textAlignVertical: TextAlignVertical.center,
          textAlign: widget.textAlign!,
          style: TextStyle(
            fontSize: 14.0.sp,
            color: ColorConf.gray,
          ),
          decoration: InputDecoration(
            hintText: widget.placeholder,
            hintStyle: TextStyle(fontSize: 14.sp, color: ColorConf.lightGray),
            prefixIcon: Padding(
              padding: EdgeInsets.only(left: 12.w, right: 10.w),
              child: RoucesUtil.getAssetsIcon('home/search', 16),
            ),
            prefixIconConstraints: constraints,
            suffixIcon: buildSuffixIcon(),
            suffixIconConstraints: constraints,
            contentPadding: EdgeInsets.zero,
            isCollapsed: true,
            border: OutlineInputBorder(
              borderSide: BorderSide.none,
              borderRadius: BorderRadius.all(Radius.circular(50)),
            ),
            isDense: true,
          ),
        ),
      ),
    );
  }
}

class AlwaysDisabledFocusNode extends FocusNode {
  @override
  // TODO: implement hasFocus
  bool get hasFocus => false;
}
